package com.atguigu.campusintelligentassistantsystem.controller;

import com.atguigu.campusintelligentassistantsystem.entity.Notice;
import com.atguigu.campusintelligentassistantsystem.entity.NoticeFavorite;
import com.atguigu.campusintelligentassistantsystem.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/notices")
@CrossOrigin(origins = "*")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 发布通知
     * @param notice 通知信息
     * @return 保存后的通知
     */
    @PostMapping
    public ResponseEntity<Notice> publishNotice(@RequestBody Notice notice) {
        Notice savedNotice = noticeService.publishNotice(notice);
        return ResponseEntity.ok(savedNotice);
    }

    /**
     * 获取所有通知（按发布时间倒序）
     * @return 通知列表
     */
    @GetMapping
    public ResponseEntity<List<Notice>> getAllNotices() {
        List<Notice> notices = noticeService.getAllNotices();
        return ResponseEntity.ok(notices);
    }

    /**
     * 根据类型获取通知
     * @param noticeType 通知类型
     * @return 通知列表
     */
    @GetMapping("/type/{noticeType}")
    public ResponseEntity<List<Notice>> getNoticesByType(@PathVariable String noticeType) {
        List<Notice> notices = noticeService.getNoticesByType(noticeType);
        return ResponseEntity.ok(notices);
    }

    /**
     * 根据ID获取通知
     * @param id 通知ID
     * @return 通知信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Notice> getNoticeById(@PathVariable Long id) {
        Notice notice = noticeService.getNoticeById(id);
        if (notice != null) {
            return ResponseEntity.ok(notice);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 更新通知
     * @param id 通知ID
     * @param notice 通知信息
     * @return 更新后的通知
     */
    @PutMapping("/{id}")
    public ResponseEntity<Notice> updateNotice(@PathVariable Long id, @RequestBody Notice notice) {
        notice.setId(id);
        Notice updatedNotice = noticeService.updateNotice(notice);
        return ResponseEntity.ok(updatedNotice);
    }

    /**
     * 删除通知
     * @param id 通知ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteNotice(@PathVariable Long id) {
        noticeService.deleteNotice(id);
        return ResponseEntity.ok("通知删除成功");
    }

    /**
     * 收藏通知
     * @param userId 用户ID
     * @param noticeId 通知ID
     * @return 收藏记录
     */
    @PostMapping("/{noticeId}/favorite")
    public ResponseEntity<NoticeFavorite> favoriteNotice(@RequestParam Long userId, @PathVariable Long noticeId) {
        NoticeFavorite favorite = noticeService.favoriteNotice(userId, noticeId);
        return ResponseEntity.ok(favorite);
    }

    /**
     * 取消收藏通知
     * @param userId 用户ID
     * @param noticeId 通知ID
     * @return 操作结果
     */
    @DeleteMapping("/{noticeId}/favorite")
    public ResponseEntity<String> unfavoriteNotice(@RequestParam Long userId, @PathVariable Long noticeId) {
        noticeService.unfavoriteNotice(userId, noticeId);
        return ResponseEntity.ok("取消收藏成功");
    }

    /**
     * 获取用户收藏的通知
     * @param userId 用户ID
     * @return 收藏列表
     */
    @GetMapping("/favorites/{userId}")
    public ResponseEntity<List<NoticeFavorite>> getUserFavorites(@PathVariable Long userId) {
        List<NoticeFavorite> favorites = noticeService.getUserFavorites(userId);
        return ResponseEntity.ok(favorites);
    }
}